﻿using Common.Contract;
using Common.Contract.Customer.Identifier;
using Common.Contract.DataCenter.Record;
using Common.Contract.DataCenter.Request;
using Common.Service.Command;
using Common.Service.Queue;
using Common.Util;
using DataCenter.Configuration;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataCenter.ServiceCommand
{
    public class SynchronizeCdcHistoryServiceCommand : ProviderServiceCommand<IProviderFactory, SynchronizeCdcHistoryRequest, ResponseRecord>
    {
        public SynchronizeCdcHistoryServiceCommand(IProviderFactory provider)
            : base(provider)
        {
        }
        protected override void Validate(SynchronizeCdcHistoryRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.Record == null)
            {
                throw new ArgumentException("Record must be set", "request");
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var cdcHistRecord = Request.Record;
            var message = new SyncCdcHistRecord
            {
                Record = cdcHistRecord,
                CreateDate = DateTime.Now,
                MessageID = Guid.NewGuid()
            };
            var messageQueue = QueueFactory.Queue;
            messageQueue.Send(message);
            return true;
        }
    }
}
